package com.stylefeng.guns.modular.pay.controller.api;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.AjaxRes;
import com.stylefeng.guns.modular.pay.dto.ApiPayParams;
import com.stylefeng.guns.modular.pay.exception.ApiException;
import com.stylefeng.guns.modular.pay.service.IJftService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 收银台
 * @author Administrator
 *
 */
@Api(value="收银台相关业务接口",tags="收银台相关业务接口")
@Controller
@RequestMapping("/api/desk")
public class ApiDeskController extends BaseController{
    private final static Logger logger = LoggerFactory.getLogger(ApiDeskController.class);
    ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private IJftService jftService;
	

	@ApiOperation(value = "商户支付", notes = "商户支付")
	@RequestMapping(value = "/quick", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes quick(ApiPayParams params) {
		AjaxRes ajax = null;
		try {
			 ajax = jftService.quickPay(params);
		} catch (ApiException e) {
			logger.error(e.getRetCode()+"|"+e.getErrMsg());
			return new AjaxRes(e.getRetCode(),e.getErrMsg());
		}catch (Exception e){
			e.printStackTrace();
			return new AjaxRes("unkonw.exception","未知异常");
		}
		return ajax;
	}
	
	@ApiOperation(value = "支付结果查询", notes = "支付结果查询")
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	@ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "String")
	@ResponseBody
	public AjaxRes query(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
		Map<String,String> params = getRequestParams(httpRequest);
		try {
			jftService.query(params);
		} catch (ApiException e) {
			return new AjaxRes(e.getRetCode(),e.getErrMsg());
		}
		return new AjaxRes(AjaxRes.SUCCESS, "");
	}
	
}
